package com.bdqn.controller;

import com.bdqn.common.Constants;
import com.bdqn.common.PageSupport;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysRoleService;
import com.bdqn.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * SysUserController
 *
 * @author LILIBO
 * @since 2021-11-04
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Resource
  SysRoleService sysRoleService;
    @Resource
    SysUserService sysUserService;

    @PostMapping("/login")
    public String login(Model model, HttpSession session, @RequestParam String account, @RequestParam String password) {
        System.out.println("account: " + account + " password: " + password);
        SysUser sysUser = sysUserService.getSysUserByAccount(account, password);
        if (sysUser == null) {
            model.addAttribute("error", "账号或密码错误");
            return "login"; // 登录失败，跳转回登录页面
        }
        // 数据库账号密码匹配成功之后，将用户对象保存到Session作用域中
        session.setAttribute("USER_SESSION", sysUser);

        return "frame"; // 登录成功，跳转到后台页面
    }
    @GetMapping("list")
    public String list(Model  model, @RequestParam(required = false) String queryRealName, @RequestParam(required = false) Long queryRoleId, @RequestParam(required = false, defaultValue = "1") String pageIndex)
    {

        // 页面查询条件数据回显（使用的查询条件在查到数据之后需要继续保留在页面输入框中）
        model.addAttribute("queryRealName", queryRealName);
        model.addAttribute("queryRoleId", queryRoleId);

        // 从服务层获取角色集合数据
        List<SysRole> sysRoleList = sysRoleService.selectSysRoleList(null);
        for (SysRole role:sysRoleList){
            System.out.println(role.getRoleName());
        }
        // 将角色列表放入Page作用域，页面通过roleList使用
        model.addAttribute("roleList", sysRoleList);

        SysUser sysUser=new SysUser();
        sysUser.setRoleId(queryRoleId);
        sysUser.setRealName(queryRealName);
        // 实例化分页查询工具类
        PageSupport page = new PageSupport();
        // 首先使用和查询相同的条件获得总记录数
        page.setTotalCount(sysUserService.getPageListTotal(sysUser)); // 获得总记录数并设置在分页对象中
        page.setCurrentPageNo(pageIndex==null ? 1:Integer.parseInt(pageIndex)); // 当前页

        // 查询分页数据
        List<SysUser> sysUserList = sysUserService.getPageList(sysUser, page);
        for (SysUser sysUser1:sysUserList){
            System.out.println(sysUser1.getRealName());
        }
        model.addAttribute("userList", sysUserList);

        // 设置分页信息
        model.addAttribute("page", page);

        return "sysUser/list";


    }
    @GetMapping("/view")
    public String view(Model model,@RequestParam long id){
        System.out.println(id);
        SysUser sysUser=sysUserService.getById(id);
        System.out.println(sysUser.toString());
        model.addAttribute("sysUser",sysUser);
      return "sysUser/view";
    }

    @ResponseBody // 直接将数据响应（通常使用Ajax调用时要加该注解）
    @DeleteMapping("/del/{userid}")
    public String del(@PathVariable("userid") long id){
        boolean delok =sysUserService.del(id);;
        if (delok==true) {
            return "{\"delResult\":\"" + delok + "\"}";
        } else {
            return "{\"delResult\":\"notexist\"}";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/toUpdate")
    public String toUpdate(@RequestParam long id, Model model) {
        System.out.println("跳转到修改用户信息页面");
        SysUser sysUser = sysUserService.getById(id);
        model.addAttribute(sysUser);
        return "sysUser/update";
    }

    @PostMapping("/upd")
    public String updd(SysUser sysUser,HttpSession session){
        System.out.println("aadfdddddddd");
        sysUser.setUpdatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());
        if(sysUserService.update(sysUser)){
            return "redirect:/user/list";
        }
       return "sysUser/update";
    }

}
